using System.Collections;
using System.Collections.Generic;
using Ants.Utilities;
using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;

[RequireComponent(typeof(CircleCollider2D))]
public class CircleShadowCollider : ShadowCollider
{

    
    //public float rayCastDist=10f;

    public LayerMask rayCastLayer;

    private EdgeCollider2D _shadowCollider = null;
    private EdgeCollider2D shadowCollider
    {
        get
        {
            if (_shadowCollider == null)
            {
                _shadowCollider = GetComponentInChildren<EdgeCollider2D>();

                if (_shadowCollider == null)
                {
                    GameObject go=new GameObject("ShadowCollider");
                    go.gameObject.layer = (int) Consts.ELayer.ShadowCollider;
                    _shadowCollider = go.AddComponent<EdgeCollider2D>();
                    go.transform.SetParent(transform,false);
                    if (go.GetComponent<Rigidbody2D>() == null)
                    {
                        Rigidbody2D rb=go.AddComponent<Rigidbody2D>();
                        rb.bodyType = RigidbodyType2D.Static;
                    }
                }
                    
                

            }
            return _shadowCollider;
        }
    }
    private CircleCollider2D _collider = null;
    private CircleCollider2D collider
    {
        get
        {
            if (_collider == null)
            {
                _collider = GetComponent<CircleCollider2D>();
            }
            return _collider;
        }
    }

    


    // Update is called once per frame
    void Update()
    {
        
        if (caster != null && caster.castsShadows && !isShadowCulled())
        {
            
            GenCollider();
        }
        
    }

    private void GenCollider()
    {
        List<Vector2> ps=new List<Vector2>(4);
        TangentInfo2D t1, t2;
        Geometry2D.GetTangentInfoPair(light.transform.position,new Circle(collider.transform.position,collider.radius),out t1,out t2);
        ps.Add(t1.worldPos);
        ps.Add(t2.worldPos);
        RaycastHit2D hit3 = Physics2D.Raycast(t2.worldPos, t2.dir,rayCastDist,rayCastLayer.value);
        Vector2 p3 =hit3.transform!=null? hit3.point : t2.worldPos + t2.dir * rayCastDist;
        RaycastHit2D hit4 = Physics2D.Raycast(t1.worldPos, t1.dir,rayCastDist,rayCastLayer.value);
        Vector2 p4 = hit4.transform!=null? hit4.point : t1.worldPos + t1.dir * rayCastDist;
        ps.Add(p3);
        ps.Add(p4);
        ps.Add(t1.worldPos);
        
        var po=new Polygon2D(ps.ToArray());
        po.InverseTransformBy(collider.transform);
        shadowCollider.SetPoints(new List<Vector2>(po.points));
        shadowCollider.edgeRadius = edgeColliderThick;
    }

    private void OnDrawGizmos()
    {
        
        TangentInfo2D t1, t2;
        Geometry2D.GetTangentInfoPair(light.transform.position,new Circle(collider.transform.position,collider.radius),out t1,out t2);
        
        RaycastHit2D hit3 = Physics2D.Raycast(t2.worldPos, t2.dir,rayCastDist,rayCastLayer.value);
        Vector2 p3 =hit3.transform!=null? hit3.point : t2.worldPos + t2.dir * rayCastDist;
        RaycastHit2D hit4 = Physics2D.Raycast(t1.worldPos, t1.dir,rayCastDist,rayCastLayer.value);
        Vector2 p4 = hit4.transform!=null? hit4.point : t1.worldPos + t1.dir * rayCastDist;
        Gizmos.DrawLine(t1.worldPos,t2.worldPos);
        Gizmos.DrawLine(t2.worldPos,p3);
        Gizmos.DrawLine(p3,p4);
        Gizmos.DrawLine(p4,t1.worldPos);
        
    }
}
